package sensor;

import grammer.components.ObjectEnum;
import houseHold.HouseHoldObject;
import houseHold.Room;
import weather.CurrentWeather;
import constants.Constants;

public class TemperatureCalculator {
	private static double inside = 18;
	private static double old = 18;

	public static double compute(Room r, int time) {
		double coef = getOutsideFactor(r);
		double electric = getElectricFactor(r);
		old = inside;
		inside = coef + electric + 18;
		inside = (old + inside) / 2;
		int variable = (int) inside;
		int avg = Constants.rd.nextInt(variable);
		boolean sw = Constants.rd.nextBoolean();
		avg = avg + 1;
		if (sw)
			avg = avg * (-1);
		double davg = 1 / ((double) avg);
		return inside + davg;
	}

	private static double lastRes = 1;

	private static double getElectricFactor(Room r) {
		HouseHoldObject radiator = r.getItemByType(ObjectEnum.RADIATOR);
		double radH = 0;
		HouseHoldObject heating = r.getItemByType(ObjectEnum.HEATING);
		double heatH = 0;
		HouseHoldObject aircond = r.getItemByType(ObjectEnum.AIR_CONDITIONING);
		double airH = 0;
		HouseHoldObject stove = r.getItemByType(ObjectEnum.STOVE);
		double stoveH = 0;
		HouseHoldObject gas = r.getItemByType(ObjectEnum.GAS_COOKER);
		double gasH = 0;
		if (radiator != null)
			if (radiator.isActive())
				radH = radiator.getPastActivity() / 60
						* radiator.getConsumptionCoefficient() / 100;
		if (heating != null)
			if (heating.isActive())
				heatH = heating.getPastActivity() / 60
						* heating.getConsumptionCoefficient() / 100;
		if (aircond != null)
			if (aircond.isActive())
				airH = aircond.getPastActivity() / 60
						* aircond.getConsumptionCoefficient() / 100;
		if (stove != null)
			if (stove.isActive())
				stoveH = stove.getPastActivity() / 60
						* stove.getConsumptionCoefficient() / 100;
		if (gas != null)
			if (gas.isActive())
				gasH = gas.getPastActivity() / 60
						* gas.getConsumptionCoefficient() / 100;
		double res = (radH * 0.05 + heatH * 0.09 + stoveH * 0.03 + gasH * 0.02 - airH * 0.09)
				* 1 / (inside + lastRes * lastRes * lastRes);
		lastRes = res;
		return res;
	}

	private static double getOutsideFactor(Room r) {
		int outside = CurrentWeather.getInstance().getTemperature();
		double coef;
		if (outside > inside) {
			coef = Math.sqrt(outside - inside) * 2;
			if (r.isWindowStatus())
				coef = coef * 1.2;
		} else {
			coef = Math.sqrt(inside - outside) * 2;
			if (r.isWindowStatus())
				coef = coef * 0.8;
		}

		return coef;
	}

	public static void main(String args[]) {
		int a = -122;
		System.out.println(a % 4);
	}
}
